// mainPackage/pages/productDetail/productDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: '', //商品
    commentList: [], //评论列表
    discussions: [], //问答列表
    formatList: [], //规格
    swiperList: [], //轮播图
    detail: [], // 详情图
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    showFormat: false,
    proNum: 0,
    selectFormat: '', //选中的规格
    productId: '',
    isCollection: false, // 是否已收藏
    tuiJianList: [],
    diamondNum: 0, //钻石数
  },
  //  立即购买  加入购物车
  buyHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    if (flag == 1) {
      // 立即购买
      var product = this.data.product,
        order = [];

      product.selectFormat = this.data.selectFormat
      product.selectFormat.factoryId = product.factoryId
      console.log(product);

      order.push(product)
      console.log(order);
      if (product.productKind == 1 && this.data.selectFormat.productScore > this.data.diamondNum) {
        wx.showModal({
          title: '抱歉',
          content: '您的钻石不足，不能兑换',
          confirmText: '获取钻石',
          complete: (res) => {
            if (res.cancel) {

            }
            if (res.confirm) {
              wx.navigateTo({
                url: '/mainPackage/pages/vipCenter/vipCenter',
              })
            }
          }
        })
        return
      }

      wx.navigateTo({
        url: '/orderPackage/pages/submitOrder/submitOrder?order=' + JSON.stringify(order),
      })


    } else if (flag == 2) {
      // 加入购物车

      console.log(this.data.product);
      console.log(this.data.selectFormat);
      this.addCart()

    }
  },

  addCart() {
    var url = app.globalData.url + "miniMain/addCart",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        factoryId: that.data.product.factoryId,
        productId: that.data.product.productId,
        formatId: that.data.selectFormat.formatId,
        cartNum: that.data.selectFormat.selectNum,
        cartPrice: that.data.selectFormat.totalMoney,
        userId: app.globalData.userId,
        productName: that.data.product.productName,
        productImage: that.data.product.productImage,
        formatName: that.data.selectFormat.formatName,
        productPrice: that.data.selectFormat.productPrice,
        productScore: that.data.selectFormat.productScore || 0,
        productNum: that.data.selectFormat.selectNum,
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.showToast({
            title: '添加成功',
          })

          console.log(res);
        } else {
          wx.showToast({
            title: '添加失败',
            icon: 'error'
          })
        }
      },
      err => {
        console.log(err);
      })


  },

  // 收藏  购物车 商品详情
  btnHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'collection') {
      // 收藏
      this.priseOrcollectHandle(1, this.data.isCollection ? 0 : 1)
    } else if (flag == 'cart') {
      // 购物车
      wx.navigateTo({
        url: '/orderPackage/pages/shoppingCart/shoppingCart',
      })
    } else if (flag == 'detail') {
      var id = e.currentTarget.dataset.id;
      wx.redirectTo({
        url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
      })
    }
  },

  // 选择规格
  selectFormat(e) {
    console.log(e);
    var obj = e.currentTarget.dataset.obj;
    this.setData({
      selectFormat: obj
    })
  },
  // 选择属性
  showHandle() {
    this.setData({
      showFormat: true
    })
  },
  // 关闭属性弹窗
  onFormatClose() {
    this.setData({
      showFormat: false
    })

  },

  // 商品数量
  onNumChange(e) {
    console.log(e);
    e.detail
    var selectFormat = this.data.selectFormat;
    selectFormat.selectNum = e.detail
    selectFormat.totalMoney = selectFormat.selectNum * selectFormat.productPrice
    this.setData({
      selectFormat
    })
    console.log(this.data);
  },


  getDetail() {
    var url = app.globalData.url + "scProduct/getDetail",
      detail = [],
      selectFormat = '',
      isCollection = this.data.isCollection,
      swiperList = [],
      tuiJianList = [],
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        productId: that.data.productId,
        // productId: 13,
        userId: app.globalData.userId
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          tuiJianList = res.productList
          isCollection = res.collectOrPrise != null
          wx.hideLoading()
          console.log(res);
          if (res.product.productVideo != null) {
            swiperList.push({
              image: res.product.productVideo,
              imageType: 2
            })
          }
          res.imgList.forEach(item => {
            if (item.imageType == 0) {
              swiperList.push(item)
            } else {
              detail.push(item)
            }
          })


          res.formatList.forEach(ele => {
            ele.selectNum = 1
            ele.totalMoney = ele.selectNum * ele.productPrice
          })

          that.setData({
            commentList: res.commentList,
            discussions: res.discussions,
            formatList: res.formatList,
            swiperList,
            detail,
            selectFormat: res.formatList[0],
            product: res.product,
            isCollection,
            tuiJianList
          })
          console.log(res);

          console.log(that.data);
        }
      },
      err => {
        console.log(err);
      })
  },

  // 查看全部评论
  toPage(e) {
    var id = this.data.productId,
      flag = e.currentTarget.dataset.flag;
    if (flag == 'comment') {
      wx.navigateTo({
        url: '/mainPackage/pages/commentList/commentList?id=' + id,
      })
    } else if (flag == 'question') {
      wx.navigateTo({
        url: '/mainPackage/pages/productAnswer/productAnswer?id=' + id,
      })
    }


  },


  // 点赞 收藏
  priseOrcollectHandle(type, status) {
    var url = app.globalData.url + "ScBaiKe/addCollectOrPrise",
      isLike = this.data.isLike,
      isCollection = this.data.isCollection,
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        productId: that.data.product.productId,
        userId: app.globalData.userId,
        flag: 0,
        type,
        status
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()
          isCollection = !isCollection
          wx.showToast({
            title: isCollection ? '已收藏' : '取消收藏',
            icon: 'none'
          })
          that.setData({
            isCollection
          })

          console.log(res);
        }
      },
      err => {
        console.log(err);
      })
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    console.log(app.globalData);

    this.setData({
      productId: options.id,
      userScore: app.globalData.userScore,
      diamondNum: app.globalData.diamondNum,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let myObj = {
      title: `商城`,
      path: "/pages/index/index",
    }
    return myObj;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    // if (e.from === "button") {
    //   let myObj = {
    //     title: `商城`,
    //     path: "/pages/index/index",
    //   }
    //   return myObj;
    // } else {

    // }
    let myObj = {
      title: `商城`,
      path: "/pages/index/index",
    }
    return myObj;

  }
})